package com.shiliao.app.activity.index.ui;

import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.shiliao.app.R;
import com.shiliao.app.activity.index.entity.AddRemindEntity;
import com.shiliao.app.activity.index.entity.WaitExamineEntity;
import com.shiliao.app.activity.index.presenter.WaitExaminePresenter;
import com.shiliao.app.activity.index.view.WaitExamineView;
import com.shiliao.app.event.MessageEvent;
import com.shiliao.app.manager.YMUserManager;
import com.yimei.framework.base.event.BaseMessageEvent;
import com.yimei.framework.base.ui.BaseFragment;
import com.yimei.framework.ui.recyclerview.CommonAdapter;
import com.yimei.framework.ui.recyclerview.base.ViewHolder;
import com.yimei.framework.util.LogUtil;

import java.util.List;

import butterknife.BindView;

/**
 * Created by yinzhiqiang on 2018/4/20.
 */

public class WaitExamineFragment extends BaseFragment implements WaitExamineView,
        SwipeRefreshLayout.OnRefreshListener{

    @BindView(R.id.recyclerview)
    RecyclerView recyclerView;

    @BindView(R.id.swip_refresh)
    SwipeRefreshLayout refresh;
    private WaitExaminePresenter waitExaminePresenter;
    private CommonAdapter  commonAdapter;
    private boolean isRefresh = false;
    @Override
    public int getLayoutId() {
        return R.layout.fragment_wait_examine;
    }

    @Override
    protected void initFragmentView() {
        super.initFragmentView();
        waitExaminePresenter=new WaitExaminePresenter(this);
        waitExaminePresenter.requestExamineList(YMUserManager.getUserId(getContext()));
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        refresh.setOnRefreshListener(this);
//        onRefresh();
//        showRefreshView();
    }


    @Override
    public void setAdapter(List list) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (list != null) {
                    recyclerView.setAdapter(new CommonAdapter<WaitExamineEntity>(getContext(),
                            R.layout.item_examine_content, list) {
                        @Override
                        protected void convert(ViewHolder holder, final WaitExamineEntity entity, int position) {
                            holder.setText(R.id.tv_title,entity.title);
                            if (!TextUtils.isEmpty(entity.status)&&entity.status.equals("0")){
                                holder.setText(R.id.tv_isexamine,"未审核");
                            }else if (!TextUtils.isEmpty(entity.status)&&entity.status.equals("1")){
                                holder.setText(R.id.tv_isexamine,"已审核");
                            }else{
                                holder.setText(R.id.tv_isexamine,"未审核");
                            }
                            holder.setOnClickListener(R.id.rl_examine_status, new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    Intent intent=new Intent(getContext(),WaitExaminDetailActivity.class);
                                    intent.putExtra("id",entity.id);
                                    intent.putExtra("type",entity.type);
                                    intent.putExtra("status",entity.status);
                                    intent.putExtra("title",entity.title);
                                    startActivity(intent);
                                }
                            });

                        }

                    });
                }

            }
        });
    }

    @Override
    public void updateView() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                isRefresh = false;
                if (refresh.isRefreshing()) {
                    refresh.setRefreshing(false);
                }
            }
        });


    }

    @Override
    public void toast() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showToast("今日要审核的都已审核完毕,谢谢!");
            }
        });
    }

    @Override
    public synchronized void onEventMainThread(BaseMessageEvent event) {
        super.onEventMainThread(event);
        if (event != null && MessageEvent.EVENT_UPDATE_WAIT_EXAMINE_LIST.equals(event.eventName)) {
            LogUtil.e("yzq","刷新");
            waitExaminePresenter.requestExamineList(YMUserManager.getUserId(getContext()));
        }
    }

    @Override
    public void onRefresh() {
        if (!isRefresh) {
            isRefresh = true;
            waitExaminePresenter.requestExamineList(YMUserManager.getUserId(getContext()));
        }
    }

    public void showRefreshView() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (refresh != null) {
                    refresh.setRefreshing(true);
                }
            }
        });
        if (refresh != null && refresh.isRefreshing()) {
            Handler handler = new Handler(Looper.getMainLooper());
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    isRefresh = false;
                    refresh.setRefreshing(false);
                }
            }, 6000);
        }
    }


}
